///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
//////////												//////////
//////////					Money system:					//////////
//////////			Created by: [TG] Chief (Alex) [TGR]				//////////
//////////			Youtube page:	www.youtube.com/humblesnurp		//////////
//////////												//////////
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
//////////												//////////
//////////			You may use/edit this script for ANYTHING!			//////////
//////////					ITS LICENCE FREE				//////////
//////////												//////////
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////

print("Money system - Client Activated")
include("base/CostSettings.lua")

ms_active = false
local ms_cFrame = nil
local ms_cash_Label = nil
local original_think = nil

util.PrecacheSound("buttons/button17.wav")
util.PrecacheSound("buttons/button10.wav")

function testframe()
	ms_cFrame = vgui.Create( "DFrame" )
	ms_cFrame:SetSize( 200,140 )
	ms_cFrame:SetPos( 10,10 )
	ms_cFrame:CenterHorizontal()
	ms_cFrame:SetTitle( "Money System" )
	--ms_cFrame:PostMessage( "SetTitle", "text", "Money System" )
	ms_cFrame:ParentToHUD()
	ms_cFrame:SetVisible( true )
	ms_cFrame:SetDraggable( true )
	ms_cFrame:ShowCloseButton( false )
	ms_cFrame:SetDeleteOnClose(false)
	ms_cFrame:MakePopup()

	local ms_cPanel = vgui.Create( "DPanel", ms_cFrame )
	ms_cPanel:SetPos( 90, 30 )
	ms_cPanel:SetSize( 95, 100 )
	ms_cPanel.Paint = function() -- Paint function
		surface.SetDrawColor( 50, 50, 50, 255 ) -- Set our rect color below us; we do this so you can see items added to this panel
		surface.DrawRect( 0, 0, ms_cPanel:GetWide(), ms_cPanel:GetTall() ) -- Draw the rect
	end
	
	ms_d_100 = vgui.Create('DButton', ms_cFrame)
	ms_d_100:SetSize(70, 20)
	ms_d_100:SetPos(15, 30)
	ms_d_100:SetText('100')
	ms_d_100.DoClick = function() SpawnMoney(100) end

	
	ms_d_1000 = vgui.Create('DButton', ms_cFrame)
	ms_d_1000:SetSize(70, 20)
	ms_d_1000:SetPos(15, 50)
	ms_d_1000:SetText('1000')
	ms_d_1000.DoClick = function() SpawnMoney(1000) end

	ms_d_10000 = vgui.Create('DButton', ms_cFrame)
	ms_d_10000:SetSize(70, 20)
	ms_d_10000:SetPos(15, 70)
	ms_d_10000:SetText('10000')
	ms_d_10000.DoClick = function() SpawnMoney(10000) end	


	ms_d_50000 = vgui.Create('DButton', ms_cFrame)
	ms_d_50000:SetSize(70, 20)
	ms_d_50000:SetPos(15, 90)
	ms_d_50000:SetText('50000')
	ms_d_50000.DoClick = function() SpawnMoney(50000) end
	
	ms_d_100000 = vgui.Create('DButton', ms_cFrame)
	ms_d_100000:SetSize(70, 20)
	ms_d_100000:SetPos(15, 110)
	ms_d_100000:SetText('100000')
	ms_d_100000.DoClick = function() SpawnMoney(100000) end


	ms_d_Label = vgui.Create( "DLabel", ms_cFrame )
	ms_d_Label:SetSize( 95, 10 )
	ms_d_Label:SetPos( 90+2, 30+2 )
	ms_d_Label:SetText( "Current Cash:" )
	ms_d2_Label = vgui.Create( "DLabel", ms_cFrame )
	ms_d2_Label:SetSize( 95, 10 )
	ms_d2_Label:SetPos( 90+2, 60+2 )
	ms_d2_Label:SetText( "Item Cost:" )
	ms_d2_Label:SetVisible(false)
	
	ms_cash_Label = vgui.Create( "DLabel", ms_cFrame )
	ms_cash_Label:SetSize( 95, 10 )
	ms_cash_Label:SetPos( 90+2, 45+2 )
	ms_cash_Label:SetText( "- LOADING -" )
	
	ms_cost_Label = vgui.Create( "DLabel", ms_cFrame )
	ms_cost_Label:SetSize( 95, 10 )
	ms_cost_Label:SetPos( 90+2, 75+2 )
	ms_cost_Label:SetText( "-" )
	ms_cost_Label:SetVisible(false)
	--ms_cash_Label:SetAlign( 1 )
	
	original_think = ms_cFrame.Think
	function ms_cFrame:Think()
		ms_cash_Label:SetText( thousandpointers(LocalPlayer():GetNWInt( "cash" )).."c" )
		if (LocalPlayer():GetNWBool( "showcurprice" )) then
			if (not ms_cost_Label:IsVisible() || not ms_d2_Label:IsVisible()) then
				ms_d2_Label:SetVisible(true)
				ms_cost_Label:SetVisible(true)
			end
			ms_cost_Label:SetText( thousandpointers(LocalPlayer():GetNWInt( "curprice" )).."c" )
		else
			if (ms_cost_Label:IsVisible() || ms_d2_Label:IsVisible()) then
				ms_d2_Label:SetVisible(false)
				ms_cost_Label:SetVisible(false)
			end
		end
		original_think(ms_cFrame)
	end

	ms_cFrame:SetKeyBoardInputEnabled(false)
	ms_cFrame:SetMouseInputEnabled(false)
	if (not GetConVar("ms_active"):GetBool()) then
		frame_close()
	end
end

function SpawnMoney(val)
	--Msg("SpawnMoney: "..tostring(val).."\n")
	RunConsoleCommand("Spwn_Mny", val);
end

local function OnContextMenuOpen( )
	if (ms_cFrame) then
		ms_cFrame:SetMouseInputEnabled(true)
	end
	--ErrorNoHalt( "Context menu opened!!\n" )
end
local function OnContextMenuClose( )
	if (ms_cFrame) then
		ms_cFrame:SetMouseInputEnabled(false)
	end
    --ErrorNoHalt( "Context menu closed!!\n" )
end

function frame_open()
	if (ms_active) then
		ms_cFrame:SetVisible(true)
	end
end
 
function frame_close()
	ms_cFrame:SetVisible(false)
end

function ms_moneyactive(um)
	--MsgN("Changed Clientside")
	if (um:ReadBool()) then
		ms_active = true
		frame_open()
	else
		ms_active = false
		frame_close()
	end
end

function thousandpointers(amount)
  local formatted = amount
  while true do  
    formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1\.%2')
    if (k==0) then
      break
    end
  end
  return formatted
end

usermessage.Hook("ms_active", ms_moneyactive)
/*
local function RecvMyUmsg( data )
	local lasts = 0
	if (data:ReadLong() == 0) then lasts = 3 else lasts = data:ReadLong() end
	--GAMEMODE.AddNotify(data:ReadString(), data:ReadShort(), lasts);
	--GAMEMODE.AddNotify("This is a really long string just to test", data:ReadShort(), lasts);
end
usermessage.Hook( "HintMessages", RecvMyUmsg );
*/
concommand.Add("-ms_frame",frame_close)
concommand.Add("+ms_frame",frame_open)
hook.Add("Initialize","frame_create",testframe)
hook.Add("OnContextMenuOpen","ms_Mouseon", OnContextMenuOpen)
hook.Add("OnContextMenuClose","ms_Mouseoff", OnContextMenuClose)


